/*global jQuery, $*/

var rateme = rateme || {};

/**
 * Qype wrapper
 * @author Thomas Junghans, Pirmin Schürmann
 * @class qype
 * @namespace rateme
 * @requires jQuery, rateme
 */
(function ($) {
    function Qype(qypeConsumerKey) {
        var self = this;
        this.qypeConsumerKey = qypeConsumerKey;
    }
    
    Qype.prototype = {
    	
/**
 * Retrieves places by name
 * @author Thomas Junghans, Pirmin Schürmann
 * @method getPlacesByName
 * @namespace rateme.qype
 * @requires jQuery, rateme
 */
        'getPlacesByName': function (address) {
	        var self = this;
			
			$('.ajax-loader').addClass('show');
			
            $.ajax({
				complete : function () {
					$('.ajax-loader').removeClass('show');
				},
                type: "GET",
                url: 'http://api.qype.com/v1/places.json?consumer_key=' + this.qypeConsumerKey + '&show=' + address.name + '&in=' + address.city + '&amp;callback=?',
                dataType: 'json',
                success: function (data) {
                    $(self).trigger('successByName', [data]);
                },
                error: function (err) {
                    alert("Error" + err);
                }
            });
        },
        
/**
 * Retrieves places by address
 * @author Thomas Junghans, Pirmin Schürmann
 * @method getPlacesByAddress
 * @namespace rateme.qype
 * @requires jQuery, rateme
 */	
		'getPlacesByAddress': function (address) {
            var self = this;
            
			$('.ajax-loader').addClass('show');
			
            $.ajax({
				complete : function () {
					$('.ajax-loader').removeClass('show');
				},
                type: "GET",
                url: 'http://api.qype.com/v1/places.json?consumer_key=' + this.qypeConsumerKey + '&amp;show=Restaurant' + '&in=' + address + '&amp;callback=?',
                dataType: 'json',
                success: function (data) {
                    $(self).trigger('successByAddress', [data]);
                },
                error: function (err) {
                    alert("Error" + err);
                }
            });
        },
 
/**
 * Retrieves places by latitude and longitude
 * @author Thomas Junghans, Pirmin Schürmann
 * @method getPlacesByLatLng
 * @namespace rateme.qype
 * @requires jQuery, rateme
 */	  
        'getPlacesByLatLng': function (myPos) {
			var self = this,
				myPosString = '';
	         
			myPosString = myPos.lat + ',' + myPos.lng;
			
			$('.ajax-loader').addClass('show');
			
			$.ajax({
				complete : function () {
					$('.ajax-loader').removeClass('show');
				},
				type: "GET",
				url: 'http://api.qype.com/v1/positions/' + myPosString + '/places.json?consumer_key=' + this.qypeConsumerKey + '&amp;show=Restaurant&amp;callback=?',
				dataType: 'json',
				success: function (data) {
					$(self).trigger('successByLatLng', [data, myPos]);
				},
				error: function (err) {
					alert(err);
				}
			});
        },
		
/**
 * Retrieves places by id. Use this to get details of a know place.
 * @author Thomas Junghans, Pirmin Schürmann
 * @method getPlaceById
 * @namespace rateme.qype
 * @requires jQuery, rateme
 */		
		'getPlaceById': function (id) {
            var self = this;

			$('.ajax-loader').addClass('show');
			
            $.ajax({
				complete : function () {
					$('.ajax-loader').removeClass('show');
				},
                type: "GET",
                url: 'http://api.qype.com/v1/places/' + id + '.json?consumer_key=' + this.qypeConsumerKey + '&amp;callback=?',
                dataType: 'json',
                success: function (data) {
                    $(self).trigger('successById', [data]);
                },
                error: function (err) {
                    alert("Error" + err);
                }
            });
        }
      
         
    };
    rateme.qype = Qype;
}(jQuery));